package com.medic.client.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.medic.entity.Assitant;
import com.medic.entity.Doctor;
import com.medic.entity.Group;
import com.medic.entity.Pharmacist;
import com.medic.entity.User;
import com.medic.entity.group.pain.Plan;
import com.medic.service.impl.GroupServiceImpl;
import com.medic.service.impl.PlanServiceImpl;
import com.medic.util.LoggerUtil;

/**
 * 计划控制器
 * 
 * @author Mr.zhang
 *
 */
@Controller
@RequestMapping("/client/plan")
@SessionAttributes({"user","doctor"})
public class PlanController {

	@Resource(name = "planService")
	private PlanServiceImpl planService;

	@Resource(name = "groupService")
	private GroupServiceImpl groupService;

	private final String groupsKey = "groups";
	private final String planKey = "plan";

	@RequestMapping("/queryByUser")
	public String query(@ModelAttribute("user") User user, int groupId, Model model) {
		String result = "";
		try {
			List<Group> groups = groupService.getGroupByUser(user.getId());
			if (groupId == 0 && groups.size() > 0) {
				groupId = groups.get(0).getId();
			}
			Plan plan = planService.getByGroupId(groupId);
			model.addAttribute(groupsKey, groups);
			model.addAttribute(planKey, plan);
			result = "/client/plan";
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params [ userId : " + user.getId() + ",groupId : "
					+ groupId + " ]");
			result = "error";
		}
		return result;
	}

	@RequestMapping("/queryByDoctor")
	public String query(@ModelAttribute("doctor") Doctor doctor,int groupId,Model model){
		String result = "";
		try {
			List<Group> groups = groupService.getGroupByDoctor(doctor.getId());
			if (groupId == 0 && groups.size() > 0) {
				groupId = groups.get(0).getId();
			}
			Plan plan = planService.getByGroupId(groupId);
			model.addAttribute(groupsKey, groups);
			model.addAttribute(planKey, plan);
			result = "/client/plan";
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params [ doctorId : " + doctor.getId() + ",groupId : "
					+ groupId + " ]");
			result = "error";
		}
		return result;
	}
	
	@RequestMapping("/queryByPhar")
	public String query(@ModelAttribute("pharmacist") Pharmacist pharmacist,int groupId,Model model){
		String result = "";
		try {
			List<Group> groups = groupService.getGroupByPharmacist(pharmacist.getId());
			if (groupId == 0 && groups.size() > 0) {
				groupId = groups.get(0).getId();
			}
			Plan plan = planService.getByGroupId(groupId);
			model.addAttribute(groupsKey, groups);
			model.addAttribute(planKey, plan);
			result = "/client/plan";
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params [ pharmacistId : " + pharmacist.getId() + ",groupId : "
					+ groupId + " ]");
			result = "error";
		}
		return result;
	}
	
	@RequestMapping("/queryByAssi")
	public String query(@ModelAttribute("assitant") Assitant assitant,int groupId,Model model){
		String result = "";
		try {
			List<Group> groups = groupService.getGroupByAssitant(assitant.getId());
			if (groupId == 0 && groups.size() > 0) {
				groupId = groups.get(0).getId();
			}
			Plan plan = planService.getByGroupId(groupId);
			model.addAttribute(groupsKey, groups);
			model.addAttribute(planKey, plan);
			result = "/client/plan";
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params [ assitantId : " + assitant.getId() + ",groupId : "
					+ groupId + " ]");
			result = "error";
		}
		return result;
	}
	
	@RequestMapping("/queryPlan")
	@ResponseBody
	public Plan query(int groupId) {
		Plan plan = null;
		try {
			plan = planService.getByGroupId(groupId);
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params [ groupId : " + groupId + " ]");
		}
		return plan;
	}
}
